package com.hecore.lagou.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

/**
 * 通用配置工具类
 */
public class CommonSettingUtil {

    public static void setOut(Job job,String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 1.3 in&out set
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        // 1.4 job submit
        boolean result=job.waitForCompletion(true);
        System.exit(result?0:1);
    }

    /**
     * 设置本地通用输出信息
     * @param job
     * @param inputPath
     * @param outPath
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws InterruptedException
     */
    public static void setLocalOut(Job job,String inputPath,String outPath) throws IOException, ClassNotFoundException, InterruptedException {
        // 1.3 in&out set
        FileInputFormat.setInputPaths(job,new Path(inputPath));
        FileOutputFormat.setOutputPath(job,new Path(outPath));
        // 1.4 job submit
        boolean result=job.waitForCompletion(true);
        System.exit(result?0:1);
    }

    public static void setSanpyZip(Configuration config){
        config.set("mapreduce.output.fileoutputformat.compress","true");
        config.set("mapreduce.output.fileoutputformat.compress.type","RECORD");
        config.set("mapreduce.output.fileoutputformat.compress.codec","org.apache.hadoop.io.compress.SnappyCodec");
    }

}
